<?php
/**
 * Created by PhpStorm.
 * User: Guangpeng Chen
 * Date: 15-9-9
 * Time: 下午5:40
 */
header("Content-type:text/xml");
include_once '/var/www/html/new/d/class/Array2XML.php';
if ($_REQUEST['test']) {
    if (!class_exists("go_sql")){
        include("/var/www/html/new/conf/le.je");
        $le=new go_sql();
        $le->connect();
    }
    function logfile($txt,$path){
        $fp = fopen($path,"a");
        flock($fp, LOCK_EX) ;
        fwrite($fp,$txt."\n");
        flock($fp, LOCK_UN);
        fclose($fp);
    }
    $ac1='16ucom';
    $pw1='c33367701511b4f6020ec61ded352059';
    $inSide = new SoapClient(null,array("location" => "http://localhost/open/openService/pft_insideMX.php","uri" => "www.16u.com?ac_16u=$ac1|pw_16u=$pw1|auth_16u=true"));
}

$data = get_show_Data('21833_3385', $inSide, 123492, 1);
echo($data);
function get_show_Data($tid_aid,$inSide, $account = '123492', $cooperation_way)
{
    list($tid, $aid) = explode('_', $tid_aid);
    if (!$tid) return false;
    $str             = <<<SQL
select a.id as lid,a.title as ltitle,a.jqts,b.title as ttitle ,a.address,
a.delaytime,b.pay,b.buy_limit_up,b.buy_limit_low,b.order_end,b.order_start,
b.delaytype,b.delaydays,b.notes,b.tprice,b.ddays,b.pid,b.getaddr,
b.cancel_auto_onMin,d.dhour from uu_land a
inner join uu_jq_ticket b on (a.id = b.landid)
left join uu_products c on(b.pid = c.id)
left join uu_land_f d on(b.id = d.tid)
where b.id=$tid and c.apply_limit = 1 and c.p_status = 0 limit 1
SQL;
    $GLOBALS['le']->query($str);
    $ticket_info = $GLOBALS['le']->fetch_assoc();
    $ticket_info['paymentType'] = ($ticket_info['pay']==1)? 'PREPAY' : 'CASHPAY'; //支付方式 PREPAY在线支付 CASHPAY景区支付
    $time               = date('Y-m-d', $_SERVER['REQUEST_TIME']);
    $get_ShowINF        = array(
        'method'    => 'ShowINF',
        'account'   => $account,
        'tid'       => $tid,
        'type'      => 2,
        'date'      => $time,
        "mulit"     => 7
    );
    ksort($get_ShowINF);
    $token_str = implode('', $get_ShowINF);
    $get_ShowINF['sign'] = md5($token_str);
    //获取场次
//    echo "http://www.12301.cc/api/pft_show/pft_show.php?".http_build_query($get_ShowINF);
    $round_list=  json_decode(file_get_contents("http://10.160.4.140/new/d/api/pft_show/pft_show.php?".http_build_query($get_ShowINF)),true);
//    print_r($round_list);
    if ($round_list['code']!=200) {
        logfile('获取场次失败:'.$round_list['msg'], '/var/www/html/new/d/logs/api/qunar/round_error.log');
        return false;
    }
    //获取分区
    $venue_id = $round_list['venue_id'];
    $zone_id  = $round_list['zone_id'];
//    echo "http://www.12301.cc/api/pft_show/pft_show_inside.php?method=GetZone&venue_id=$venue_id&type=1&zone_id=$zone_id";
    $zone_list =  json_decode(file_get_contents("http://10.160.4.140/new/d/api/pft_show/pft_show_inside.php?method=GetZone&venue_id=$venue_id&type=1&zone_id=$zone_id"),true);
    if ($zone_list['code']!=200) {
        logfile('获取分区失败:'.$zone_list['msg'], 'var/www/html/new/d/logs/api/qunar/round_error.log');
        return false;
    }

    $product_info = array();
    $searchday = 7;
    $validType = 'USE_DATE';    // 当天有效
    $daysAfterUseDateValid = 1;
//    print_r($round_list);exit;
    foreach($round_list['rounds'] as $date=>$rounds){
        $minimum =  1;  //最少购买票数
        $maximum =  $zone_list['seat_count'];  //最多购买票数
        $marketPrice = $ticket_info['tprice'] * 100; //市场价
        $calendarPriceList = array();
//        $i_startime = $_SERVER['REQUEST_TIME'];
//        $i_endtime  = $i_startime + 86400*$searchday;

        $lprice = $inSide->get_Dynamic_Price_Merge($ticket_info['pid'],$date,1,"","",1,"");//marketPrice票面价
        if($lprice == -1) continue;
//                $sellPrice_data =$inSide->Dynamic_Price_And_Storage($account, $ticket_info['pid'],$itime,1,0,1,$aid); //查询去哪儿结算价
//                if($sellPrice_data == 105 || $sellPrice_data ==1065 || $sellPrice_data ==103) continue;
//                $sellPrice_data_xml =simpleXML_load_string($sellPrice_data);
//                $sellPrice = (string)$sellPrice_data_xml->Rec->UUsprice;
//                if($maximum ==0) continue;
        $calendarPriceList[]['calendarPrice'] = array(
            'useDate'       => $date,
            'marketPrice'   => $marketPrice,
            'sellPrice'     => $lprice,
            'sellstock'     => $maximum,
            'minimum'       => $minimum,
            'maximum'       => $maximum,
        );
        foreach($rounds as $round) {
//            print_r($zone_list);exit;
            foreach ($zone_list['zone_list'] as $zone_info) {
                list($zone_id, $zone_name) = array_values($zone_info);
                $resource_id = "{$tid}_{$aid}_{$round_list['venue_id']}_{$round['round_id']}_{$zone_id}";
                $product_name= "{$round['round_name']}-{$zone_name}-{$ticket_info['ttitle']}";
                $product_info[] = array(
                        'baseInfo'=>array(
                            'resourceId'=>$resource_id,
                            'sights'    => array(
                                'sightName'     =>$ticket_info['ltitle'],
                                'sightAddress'  =>$ticket_info['address'],
                                'city'          =>$ticket_info['address'],
                                'sightlon'      =>'',
                                'sightlat'      =>'',
                            ),
                            'productName'=> $product_name,
                        ),
                        'bookConfig'=>array(
                            'paymentType'=>$ticket_info['paymentType'],
                            'advanceOption'=>array(
                                'bookAdvanceDay'    => $ticket_info['ddays'] > 0 ? '' : $ticket_info['ddays'],
                                'bookAdvanceTime'   => $ticket_info['dhour'],
                                'useAdvanceHour'    => explode('|',$ticket_info['delaytime']),
                            ),
                            'autoCancelTime'=> $ticket_info['cancel_auto_onMin'],
                            'contactPersonRequired'=>array(
                                'nameRequired'      => TRUE,
                                'namePinyinRequired'=>'',
                                'emailRequired'     =>'',
                                'addressRequired'   =>'',
                            ),
                            'bookPersonType'=>'CONTACT_PERSON',
                            'visitPersonRequiredForQuantity'=>999,
                            'visitPersonRequired'=>array(
                                'nameRequired'=>TRUE,
                                'namePinYinRequired'=>'',
                                'credentialsRequired'=>'',
                                'credentialsRequiredTypes'=>'',
                                'userRequiredDefined1'=>'',
                                'userRequiredDefined2'=>'',
                            ),
                            'perPhoneMaximum'=>'',
                            'perPhoneMaximumType'=>'',
                        ),
                        'priceConfig'=>array(
                            'validType'             => $validType,
                            'daysAfterUseDateValid' => $daysAfterUseDateValid,
                            'daysAfterBookDateValid'=>'',
                            'dayUseTimePeriod'  =>'',
                            'calendarPrices'    => $calendarPriceList,
                            'periodPrice'       => '',
                        ),
                        'promoteConfig'=>array(
                            'cashBackMoney'=>'',
                        ),
                        'productDescription'=>array(
                            'feeInfo'=>$ticket_info['notes'],
                            'feeInclude'=>'',
                            'feeExclude'=>'',
                            'pickupTime'=>'',
                            'pickupAddress'=>'',
                            'remind'=> $ticket_info['jqts'],
                            'refundOption'=>array(
                                'canRefund'=>TRUE,
                                'refundApplyTimeBeforeValidEndDay'=>'',
                                'refundChargeType'=>'ORDER',
                                'refundCharge'=>100,
                                'refundInfo'=>'演出当天不可退票，逾期不退。',
                            ),
                        ),
                        'otherConfig'=>array(
                            'telephone'=>'',
                            'salePeriods'=> array(
                                'salePeriod'=> array(
                                    'saleStartTime'=>'',
                                    'saleEndTime'=>'',
                                )
                            ),
                            'smsTemplet'=>'您将会在3-5分钟内收到电子票，在景区售票处取票入园。',
                            'eticketType'=>'S_STRING',
                        ),
                );
            }
        }
    }
//    var_export($product_info);
    $productInfo = '';
    $cnt = count($product_info);
    foreach($product_info as $info) {
        $xmlObj = Array2XML::createXML('productInfo', $info);
        $productInfo .= str_replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n",'', $xmlObj->saveXML());
    }
    $createTime = date('Y-m-d H:i:s', $_SERVER['REQUEST_TIME']);
    return <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<response xsi:schemaLocation="http://piao.qunar.com/2013/QMenpiaoResponseSchema QMResponseDataSchema-2.0.1.xsd" xmlns="http://piao.qunar.com/2013/QMenpiaoResponseSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	<header>
		<application>Qunar.Menpiao.Agent</application>
		<processor>SupplierDataExchangeProcessor</processor>
		<version>v2.0.1</version>
		<bodyType>GetProductByQunarResponseBody</bodyType>
		<createUser>SupplierSystemName</createUser>
		<createTime>{$createTime}</createTime>
		<code>1000</code>
		<describe>成功</describe>
	</header>
	<body xsi:type="GetProductByQunarResponseBody">
		<count>$cnt</count>
		<productInfos>
		    {$productInfo}
		</productInfos>
    </body>
</response>
XML;
}